package com.huluobo.a2207douyindemo.mine;

import android.content.Intent;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.SPUtils;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.huluobo.a2207douyindemo.R;
import com.huluobo.a2207douyindemo.adapter.CommonPagerAdapter;
import com.huluobo.a2207douyindemo.databinding.FragmentMineBinding;
import com.huluobo.a2207douyindemo.setting.SettingActivity;
import com.huluobo.a2207douyindemo.utils.ImageUtils;
import com.huluobo.base.bean.UserBeanResult;
import com.huluobo.base.db.AppDBUtils;
import com.huluobo.base.mvvm.BaseMVVMFragment;
import com.huluobo.base.net.Const;

import java.util.ArrayList;
import java.util.List;

public class MineFragment extends BaseMVVMFragment<FragmentMineBinding, MineViewModel> {
    private ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();
    private CommonPagerAdapter commonPagerAdapter;
    private List<Fragment> fragmentList = new ArrayList<>();
    private UserBeanResult.UserBean userBean;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {
        userBean =
                AppDBUtils.getInstance().getUserDao().queryUserInfoByName(SPUtils.getInstance().getString(Const.PARAM_USERNAME));
        ImageUtils.getInstance().loadImage(requireContext(), userBean.getIcon(), binding.mineUserIconIv);
        binding.mineNicknameTv.setText(userBean.getNickname());
    }

    @Override
    protected void initView() {
        tabEntities.clear();
        tabEntities.add(new TabEntity("点赞"));
        tabEntities.add(new TabEntity("收藏"));
        binding.mineTabLayout.setTabData(tabEntities);

        fragmentList.clear();
        fragmentList.add(new LikeFragment());
        fragmentList.add(new CollectFragment());

        commonPagerAdapter = new CommonPagerAdapter(requireActivity(), fragmentList);
        binding.mineVp2.setAdapter(commonPagerAdapter);

        binding.mineTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                binding.mineVp2.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        binding.mineVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.mineTabLayout.setCurrentTab(position);
            }
        });

        binding.mineSettingIv.setOnClickListener(v -> {
            Intent intent = new Intent(requireContext(), SettingActivity.class);
            intent.putExtra("userBean", userBean);
            startActivity(intent);
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        userBean =
                AppDBUtils.getInstance().getUserDao().queryUserInfoByName(SPUtils.getInstance().getString(Const.PARAM_USERNAME));
        ImageUtils.getInstance().loadImage(requireContext(), userBean.getIcon(), binding.mineUserIconIv);
    }

    @Override
    protected Class<MineViewModel> getViewModel() {
        return MineViewModel.class;
    }
}